//
//  RegisteredViewModel.m
//  CBT
//
//  Created by Mr.Zhang on 2018/11/28.
//  Copyright © 2018 Mr.Zhang. All rights reserved.
//

#import "RegisteredViewModel.h"

#import "LoginManager.h"

@implementation RegisteredViewModel

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setUp];
    }
    return self;
}

- (void)setUp{
    
    _registeredCommand =  [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        if([[input allKeys] containsObject:@"email"]){
            if ([NSString isBlankString:input[@"email"]]) {
                [HFStatusView showFailureMessage:kLocalizedString(@"请输入邮箱",nil)];
                return [RACSignal error:nil];
            }else if (![NSString validateEmail:input[@"email"]]){
                [HFStatusView showFailureMessage:kLocalizedString(@"请输入正确邮箱",nil)];
                return [RACSignal error:nil];
            }else if ([NSString isBlankString:input[@"code"]]){
                [HFStatusView showFailureMessage:kLocalizedString(@"请输入验证码",nil)];
                return [RACSignal error:nil];
            }else if ([NSString isBlankString:input[@"password"]]){
                [HFStatusView showFailureMessage:kLocalizedString(@"请输入密码",nil)];
                return [RACSignal error:nil];
            }else if ([NSString stringWithFormat:@"%@",input[@"password"]].length < 8 || [NSString stringWithFormat:@"%@",input[@"password"]].length > 20){
                [HFStatusView showFailureMessage:kLocalizedString(@"密码长度错误",nil)];
                return [RACSignal error:nil];
            }else if ([NSString isBlankString:input[@"avatar"]]){
                [HFStatusView showFailureMessage:kLocalizedString(@"请输入邀请码",nil)];
                return [RACSignal error:nil];
            }
        }else if([[input allKeys] containsObject:@"phone"]){
            if ([NSString isBlankString:input[@"phone"]]) {
                [HFStatusView showFailureMessage:kLocalizedString(@"请输入手机号",nil)];
                return [RACSignal error:nil];
            }else if (![NSString isInputRightPhoneNum:input[@"phone"]]){
                [HFStatusView showFailureMessage:kLocalizedString(@"请输入正确手机号",nil)];
                return [RACSignal error:nil];
            }else if ([NSString isBlankString:input[@"code"]]){
                [HFStatusView showFailureMessage:kLocalizedString(@"请输入验证码",nil)];
                return [RACSignal error:nil];
            }else if ([NSString isBlankString:input[@"password"]]){
                [HFStatusView showFailureMessage:kLocalizedString(@"请输入密码",nil)];
                return [RACSignal error:nil];
            }else if ([NSString stringWithFormat:@"%@",input[@"password"]].length < 8 || [NSString stringWithFormat:@"%@",input[@"password"]].length > 20){
                [HFStatusView showFailureMessage:kLocalizedString(@"密码长度错误",nil)];
                return [RACSignal error:nil];
            }else if ([NSString isBlankString:input[@"avatar"]]){
                [HFStatusView showFailureMessage:kLocalizedString(@"请输入邀请码",nil)];
                return [RACSignal error:nil];
            }
        }
        
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            
            [LoginManager phoneaAndEmailRegisteredWithID:[[input allKeys] containsObject:@"email"] ? input[@"email"] : input[@"phone"] area:input[@"area"] isPhone:[[input allKeys] containsObject:@"phone"] code:input[@"code"] pwd:input[@"password"] avatar:[NSString excludeNullString:input[@"avatar"]] isSetPassword:[input[@"type"]intValue] == 1 block:^(id data, HttpRequestResult result, NSString *msg) {
                if (result == kHttpRequestResultNormal) {
                    if ([input[@"type"]intValue] == 1) {
                        [HFStatusView showSuccessMessage:kLocalizedString(@"密码找回成功",nil)];
                    }else{
                        
                        [[NSUserDefaults standardUserDefaults]setObject:[data[@"data"]objectForKey:@"token"] forKey:HEFANTV_TOKEN];
                        [[NSUserDefaults standardUserDefaults]setObject:input[@"password"] forKey:HEFANTV_PassWord];
                        
                        [[CBTLocalUserManager sharedInstance] saveALLUserInfoDic:data];
                        
                        if (![NSString isBlankString:[data[@"data"]objectForKey:@"picUrl"]]) {
                            
                            dispatch_async(dispatch_get_global_queue(0, 0), ^{
                                
                                [CBTImageTools saveUserImageWithImage:[UIImage sd_animatedGIFWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",kBaseURL_PICSTATIC,[data[@"data"]objectForKey:@"picUrl"]]]]] imageName:[NSString stringWithFormat:@"%@/%@",kBaseURL_PICSTATIC,[data[@"data"]objectForKey:@"picUrl"]] userID:[data[@"data"]objectForKey:@"userId"]];
                            });
                        }
                    }
                    [subscriber sendNext:data];
                    [subscriber sendCompleted];
                }else{
                    [HFStatusView showFailureMessage:msg];
                    [subscriber sendError:nil];
                    [subscriber sendCompleted];
                }
            }];
            
            return [RACDisposable disposableWithBlock:^{
                NSLog(@"%@注册操作结束了",[[input allKeys] containsObject:@"email"] ? @"邮箱" : @"手机号");
            }];
        }];
    }];
    
    
    _sendCodeCommand =  [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        if([[input allKeys] containsObject:@"email"]){
            if ([NSString isBlankString:input[@"email"]]) {
                [HFStatusView showFailureMessage:kLocalizedString(@"请输入邮箱",nil)];
                return [RACSignal error:nil];
            }else if (![NSString validateEmail:input[@"email"]]){
                [HFStatusView showFailureMessage:kLocalizedString(@"请输入正确邮箱",nil)];
                return [RACSignal error:nil];
            }
        }else if([[input allKeys] containsObject:@"phone"]){
            if ([NSString isBlankString:input[@"phone"]]) {
                [HFStatusView showFailureMessage:kLocalizedString(@"请输入手机号",nil)];
                return [RACSignal error:nil];
            }else if (![NSString isInputRightPhoneNum:input[@"phone"]]){
                [HFStatusView showFailureMessage:kLocalizedString(@"请输入正确手机号",nil)];
                return [RACSignal error:nil];
            }
        }
        
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            
            [LoginManager sendPhoneAndEmailCodeWithID:[[input allKeys] containsObject:@"email"] ? input[@"email"] : input[@"phone"] area:input[@"code"] isPhone:[[input allKeys] containsObject:@"phone"] type:[input[@"type"]intValue] == 2 ? @"2" : @"1"  block:^(id data, HttpRequestResult result, NSString *msg) {
                if (result == kHttpRequestResultNormal) {
                    [HFStatusView showSuccessMessage:kLocalizedString(@"验证码发送成功",nil)];
                    [subscriber sendNext:data];
                    [subscriber sendCompleted];
                }else{
                    [HFStatusView showFailureMessage:msg];
                    [subscriber sendError:nil];
                    [subscriber sendCompleted];
                }
            }];
            
            return [RACDisposable disposableWithBlock:^{
                NSLog(@"%@发送验证码操作结束了",[[input allKeys] containsObject:@"email"] ? @"邮箱" : @"手机号");
            }];
        }];
    }];
    
}


@end
